/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: AliceMana
 * Date: 2023-01-01
 * Time: 1:13
 */
// https://leetcode.cn/problems/first-bad-version/
public class Solution2 {
    static boolean isBadVersion(int version) {
        return version == bad;
    }

    public static int firstBadVersion(int n) {
        int l = 0;
        int r = n;
        while (l <= r) {
            int mid = l+(r-l)/2;
            if (!isBadVersion(mid)) {
                if (!isBadVersion(mid) && isBadVersion(mid+1)) {
                    return mid+1;
                }
                l = mid;
            } else {
                if (isBadVersion(mid) && !isBadVersion(mid-1)) {
                    return mid;
                }
                r = mid;
            }
        }
        return -1;
    }

    public static final int bad = 4;
    public static void main(String[] args) {

    }
}
